﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.Logic;
using Cosmetic.DataEntity;

namespace WebApp
{
    public partial class Home : System.Web.UI.Page
    {
        private CategoryManager m_CategoryManager = new CategoryManager();
        private PlateManager PM = new PlateManager();
        private articleManger articlemanger = new articleManger();
        private articleclassManger articleClassManager = new articleclassManger();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Category theProductTopCategory = m_CategoryManager.GetTopCategryByName(CategoryManager.ETopCategory.Category_L);


                Repeater1.DataSource = m_CategoryManager.GetSubCateogoriesBySupCategory(theProductTopCategory).OrderBy(theCategory => theCategory.Index);
                Repeater1.DataBind();

                //this.rptMainCategory.DataSource = m_CategoryManager.GetSubCateogoriesBySupCategory(theProductTopCategory).OrderBy(theCategory => theCategory.Index);
                //this.rptMainCategory.DataBind();

                //HiddenField hfCategoryId = e.Item.FindControl("hfSubCategoryId") as HiddenField;
                //HyperLink hlcategory = e.Item.FindControl("HLSubCategoryId") as HyperLink;
                //if (hfCategoryId != null && hlcategory != null)
                //{
                //    string upCategoryId = m_CategoryManager.GetCategoryById(new Guid(hfCategoryId.Value)).SupCategoryId.ToString();

                //    hlcategory.NavigateUrl = "~/Product/ProductCategory.aspx?CID=" + upCategoryId + "&SID=" + hfCategoryId.Value;
                //}
                //商城推荐
                this.ListView1.DataSource = PM.GetPlateCosmeticProducts().Where(c => c.eprice.ToString() == "0.01");
                this.ListView1.DataBind();

                //家用推荐
                this.ListView3.DataSource = PM.GetPlateCosmeticProducts().Where(c => c.eprice.ToString() != "0.01");
                this.ListView3.DataBind();

                this.ListView5.DataSource = PM.Getxinpinpro();//新品
                this.ListView5.DataBind();
                this.ListView6.DataSource = PM.Gethotpro();//最近销售
                this.ListView6.DataBind();
                Bind1();
            }
        }

        private void Bind1()
        {
            List<articleclass> classes = articleClassManager.GetAll();

            articleclass class1 = classes.Where(c => c.name == "商城新闻").Single();

            this.rptNews.DataSource = articlemanger.GetAlls(class1.id).Take(5);
            this.rptNews.DataBind();

            List<articleclass> Active_classes = articleClassManager.GetAll();
            articleclass class2 = Active_classes.Where(c => c.name == "活动新闻").Single();

            rptActiveNews.DataSource = articlemanger.GetAlls(class2.id).Take(5);
            rptActiveNews.DataBind();
        }

        protected void rptMainCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater theRptCategory = (Repeater)e.Item.FindControl("rptSubCategory");
                Category theSupCategory = e.Item.DataItem as Category;

                List<Category> LC = this.m_CategoryManager.GetSubCateogoriesBySupCategory(theSupCategory).OrderBy(theCategory => theCategory.Index).ToList();
                if (LC.Count > 0)
                {
                    theRptCategory.DataSource = LC;
                    theRptCategory.DataBind();
                }
            }
        }
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rptlt = e.Item.FindControl("rptLittleThirdSub") as Repeater;

                Repeater rptsub1 = e.Item.FindControl("rptSub1") as Repeater;//二级目录
                //绑定二级目录
                Category theSupCategory = e.Item.DataItem as Category;
                List<Category> LC = this.m_CategoryManager.GetSubCateogoriesBySupCategory(theSupCategory).OrderBy(theCategory => theCategory.Index).ToList();
                if (LC.Count > 0)
                {
                    List<Category> lcTrimed = LC.Take(3).ToList();
                    foreach (Category item in lcTrimed)
                    {
                        if (item.Name.Length > 4)
                        { 
                            item.Name = item.Name.Substring(0, 4);
                        }
                    }

                    rptlt.DataSource = lcTrimed;
                    rptlt.DataBind();

                    rptsub1.DataSource = LC.ToList();
                    rptsub1.DataBind();
                }
            }
        }

        protected void rptLittleThirdSub_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {


        }

        protected void rptSub1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Repeater rptThirdSub2 = e.Item.FindControl("rptThirdSub2") as Repeater;//三级目录
            Category theSupCategory = e.Item.DataItem as Category;

            List<Category> LC = this.m_CategoryManager.GetSubCateogoriesBySupCategory(theSupCategory).OrderBy(theCategory => theCategory.Index).ToList();
            if (LC.Count > 0)
            {
                rptThirdSub2.DataSource = LC;
                rptThirdSub2.DataBind();
            }
        }


        protected void rptThirdSub2_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField hfCategoryId = e.Item.FindControl("hfSubCategoryId") as HiddenField;
            HyperLink hlcategory = e.Item.FindControl("HLSubCategoryId") as HyperLink;
            if (hfCategoryId != null && hlcategory != null)
            {
                string upCategoryId = m_CategoryManager.GetCategoryById(new Guid(hfCategoryId.Value)).SupCategoryId.ToString();

                hlcategory.NavigateUrl = "~/Product/ProductCategory.aspx?CID=" + upCategoryId + "&SID=" + hfCategoryId.Value;
            }

        }


        public string spliteStr(string title)
        {
            if (title.Length > 10)
            {
                title = title.Substring(0, 10) + "…";
            }
            return title;
        }
        public string spliteStrs(string title)
        {
            if (title.Length > 15)
            {
                title = title.Substring(0, 15) + "…";
            }
            return title;
        }

        protected void ListView3_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            HiddenField lbprice = e.Item.FindControl("HFePrice") as HiddenField;
            HyperLink hl = e.Item.FindControl("gvHLePrice") as HyperLink;
            Image img = e.Item.FindControl("Image9") as Image;

            //hfePrice.Value != "" && decimal.Parse(hfePrice.Value) == decimal.Parse("0.01")
            if (lbprice.Value != "" && decimal.Parse(lbprice.Value) == decimal.Parse("0.01"))
            {
                img.Visible = true;
                hl.Visible = false;
            }
            else
            {
                img.Visible = false;
                hl.Visible = true;
            }
        }
    }
}